package com.detu.ivos.dict.service.impl;

import com.detu.ivos.base.exception.ServiceException;
import com.detu.ivos.base.response.StatusCode;
import com.detu.ivos.dict.mapper.DictMapper;
import com.detu.ivos.dict.pojo.dto.DictQuery;
import com.detu.ivos.dict.pojo.dto.DictSaveParam;
import com.detu.ivos.dict.pojo.entity.Dict;
import com.detu.ivos.dict.pojo.vo.DictVO;
import com.detu.ivos.dict.service.DictService;
import com.detu.ivos.dictoption.mapper.DictOptionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class DictServiceImpl implements DictService {
    @Autowired
    DictMapper dictMapper;
    @Override
    public List<DictVO> selectDict(DictQuery dictQuery) {
        List<DictVO> dictVOS=dictMapper.select(dictQuery);
        log.debug("通过{}条件查到了{}",dictQuery,dictVOS);
        return dictVOS;
    }

    @Override
    public void savedict(DictSaveParam dictSaveParam) {
        Dict dict = new Dict();
        BeanUtils.copyProperties(dictSaveParam,dict);
        if (dict.getId()==null){
            dict.setCreateTime(new Date());
            int i =dictMapper.addDict(dict);
//            if (i<=0){
//                throw new ServiceException(StatusCode.OPERATION_FAILED);
//            }
        }else{
            int i=dictMapper.updateDict(dict);
//            if (i<=0){
//                throw new ServiceException(StatusCode.OPERATION_FAILED);
//            }
        }

    }

    @Override
    public void deleteDict(Long id) {
        log.debug("删除字典业务，参数：{}", id);
        dictMapper.deleteById(id);
        dictMapper.deleteDictOptionById(id);
    }
}
